<?php

namespace Mcm\RepasBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

use Mcm\RepasBundle\Entity\Repas;
use Mcm\RepasBundle\Entity\Paiement;

class PaiementController extends Controller
{
    
    
    public function paiementAction($idrepas){
        $user = $this->container->get('security.context')->getToken()->getUser();
        if( ! is_object($user) )
        {
            

            
            throw new AccessDeniedException('Vous n\'êtes pas authentifié.');
            $this->redirect($this->generateUrl("repas_index"));
        }else{
            $coderepas ="";
            
            while (strlen($coderepas)< 8) {
                $coderepas .= mt_rand(0, 9);
            }
            
            
            $em = $this->getDoctrine()->getManager();
            $repas = $em->getRepository('McmRepasBundle:Repas')->find($idrepas);
            $paiement  = new Paiement();
            $paiement->setCoderepas($coderepas);
            $paiement->setEtat('payé');
            $paiement->setInvite($user);
            $paiement->setRepas($repas);
            $em->persist($paiement);
            $repas->addPaiement($paiement);
            $em->flush();
            
            return $this->render('McmRepasBundle:Paiement:paiement.html.twig', array('code' => $coderepas));
        }
 
    }
}
